// pages/index/designer.js
var app = getApp();
var page = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clickId: -1,
    clickIds: -1,
    clickIdse: -1,
    thecity: "地区",
    style: "擅长风格",
    newactive: "排序",
    qyopen: false,
    qyshow: false,
    nzopen: false,
    pxopen: false,
    nzshow: false,
    pxshow: false,  
    isfull: false,
    select1: '',
    select2: '',
    shownavindex: '',
    city: '',
    styles: '',
    sort: '',
    areaID: 0,
    styleID: 0,
    sortID: 0,
    page: 1,
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var id = options.id
    console.log(id)
    if ((id == null) || (id == 0)||(id == undefined)){
      id =0
      that.setData({
        shownav:true,
        top:'120rpx'
      })
      wx.setNavigationBarTitle({
        title: '设计师' 
      })
    }else{
      that.setData({
        shownav: false,
        top: 0
      })
      wx.setNavigationBarTitle({
        title: '设计团队' 
      })
    }
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      data: {companyID:id},  
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      success: function(res) {
        console.log(res.data)
        var polist = res.data.data
        if (polist.length==0){
          that.setData({
            alertmess: true,
            message: '设计团队暂时为空，请去pc页面添加吧！'
          })
          that.settime()
        }
        that.setData({
          polist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.navconfig()

    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      id:id,
      shownavindex: 0
    })
  },
  //对话隐藏
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 3000)
  },
  //导航配置
  navconfig: function(e) {
    var that = this
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getconfig'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        console.log('请求成功', res.data)
        var city = res.data.data.area
        var style = res.data.data.style
        var sort = res.data.data.sort
        that.setData({
          city: city,
          styles: style,
          sort: sort
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
  },

  listqy: function(e) {
    if (this.data.qyopen) {
      this.setData({
        qyopen: false,
        nzopen: false,
        pxopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: false,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        content: this.data.city,
        qyopen: true,
        pxopen: false,
        nzopen: false,
        nzshow: true,
        pxshow: true,
        qyshow: false,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }
    console.log(e.currentTarget.dataset)
  },
  list: function(e) {
    if (this.data.nzopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        nzopen: true,
        pxopen: false,
        qyopen: false,
        nzshow: false,
        pxshow: true,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }
    console.log(e.currentTarget.dataset)
  },
  listpx: function(e) {
    if (this.data.pxopen) {
      this.setData({
        nzopen: false,
        pxopen: false,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: false,
        shownavindex: 0
      })
    } else {
      this.setData({
        nzopen: false,
        pxopen: true,
        qyopen: false,
        nzshow: true,
        pxshow: false,
        qyshow: true,
        isfull: true,
        shownavindex: e.currentTarget.dataset.nav
      })
    }
    console.log(e.target)
  },
  selectleft: function(e) {

    this.setData({
      cityright: {},
      citycenter: this.data.cityleft[e.currentTarget.dataset.city],
      select1: e.target.dataset.city,
      select2: ''
    });
  },
  selectcenter: function(e) {

    this.setData({
      cityright: this.data.citycenter[e.currentTarget.dataset.city],
      select2: e.target.dataset.city
    });
  },
  hidebg: function(e) {
    this.setData({
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0
    })
  },
  //切换点击
  choose_city: function(e) {
    var that = this
    var index = parseInt(e.currentTarget.id);
    var value = that.data.city[index];
    var areaID = that.data.areaID
    var styleID = that.data.styleID
    var sortID = that.data.sortID
    var page = that.data.page
    var id = that.data.id
    if (this.data.clickId == e.currentTarget.id) {
      this.setData({
        clickId: +1
      })
      return;
    }
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      data: {
        area: index,
        style: styleID,
        sort: sortID,
        page: page,
        companyID:id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.data
        that.setData({
          polist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      clickId: e.currentTarget.id,
      thecity: value,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
      areaID: index
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  choose_style: function(e) {
    var that = this
    var areaID = that.data.areaID
    var styleID = that.data.styleID
    var sortID = that.data.sortID
    var page = that.data.page
    var index = parseInt(e.currentTarget.id);
    var value = that.data.styles[index];
    var id = that.data.id
    if (that.data.clickIds == e.currentTarget.id) {
      this.setData({
        clickIds: +1
      })
      return;
    }
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      data: {
        area: areaID,
        style: index,
        sort: sortID,
        page: page,
        companyID:id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        var polist = res.data.data
        that.setData({
          polist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      clickIds: e.currentTarget.id,
      style: value,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
      styleID: index
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    console.log(value)
  },
  choose_new: function(e) {
    var that = this
    var areaID = that.data.areaID
    var styleID = that.data.styleID
    var sortID = that.data.sortID
    var page = that.data.page
    var index = parseInt(e.currentTarget.id);
    var value = that.data.sort[index];
    var id = that.data.id
    if (that.data.clickIdse == e.currentTarget.id) {
      this.setData({
        clickIdse: +1
      })
      return;
    }
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      data: {
        area: areaID,
        style: styleID,
        sort: index,
        page: page,
        companyID: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success: function(res) {
        console.log(res.data)
        var polist = res.data.data
        that.setData({
          polist: polist
        })
      },
      fail: function(res) {
        console.log('请求失败', res.data)
      }
    })
    that.setData({
      clickIdse: e.currentTarget.id,
      newactive: value,
      qyopen: false,
      nzopen: false,
      pxopen: false,
      nzshow: true,
      pxshow: true,
      qyshow: true,
      isfull: false,
      shownavindex: 0,
      sortID: index
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    console.log(value)
  },
  //阻止默认事件
  stopmove: function(e) {

  },
  skipTo: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "designer_detail?id=" + id
    })
    console.log(id);
  },
  //上拉加载
  onReachBottom: function() {
    var that = this
    that.setData({
      page: that.data.page + 1 //后台数据需要分页，每次加载一页
    })
    wx.showLoading({ //显示toast
      title: '加载中...',
    });
    var areaID = that.data.areaID
    var styleID = that.data.styleID
    var sortID = that.data.sortID
    var page = that.data.page
    var length = that.data.clickId;
    var id = that.data.id
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      data: {
        area: areaID,
        sort: sortID,
        style: styleID,
        companyID: id,
        page: that.data.page
      },
      method: 'GET',
      success: function(res) {
        if (res.data.status == 1) {
          var polist = that.data.polist
          for (var i = 0; i < res.data.data.length; i++) {
            polist.push(res.data.data[i]);
          }
          that.setData({
            polist: polist
          });
          page++
        } else { //res.data.status == 0 没有新数据了
          that.setData({
            pageEnd: true, //显示页底信息
          })
        }
        console.log(page)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        wx.hideLoading(); //隐藏toast
        // setTimeout(function() {
        //   wx.showToast({
        //     title: '没有更多数据了！',
        //   })
        // }, 2000)
      }
    })
  },
  //下拉刷新
  onPullDownRefresh: function() {
    var that = this
    var areaID = that.data.areaID
    var styleID = that.data.styleID
    var sortID = that.data.sortID
    var page = that.data.page
    var id = that.data.id
    // wx.showLoading({
    //   title: '数据加载中',
    // })
    wx.request({
      url: app.apiUrl('c=shejishi/index&a=getlist'),
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        area: areaID,
        sort: sortID,
        style: styleID,
        companyID: id
        //page: page
      },
      success: function(res) {
        var polist = res.data.data
        that.setData({
          polist: polist
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        setTimeout(function() {
          wx.hideLoading()
        }, 2000)
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },

})